package com.lyn.mvp.model

/**
 * @author Lyn
 * @date 2022/7/13
 * @desc
 */
open class Result {

    var code: Int? = 0;

    var msg: String? = null

    var data: Any? = null

    var methodName: String? = null

    companion object {
        const val SUCCESS = 1
        const val FAIL = 0;
        fun success(): Result {
            return Result().apply {
                code = SUCCESS
                msg = "success"
                methodName = getMethodName()
            }
        }

        fun success(datas: Any): Result {
            return Result().apply {
                code = SUCCESS
                msg = "success"
                data = datas
                methodName = getMethodName()
            }
        }

        fun success(message: String): Result {
            return Result().apply {
                code = SUCCESS
                msg = message
                methodName = getMethodName()
            }
        }

        fun success(message: String, datas: Any): Result {
            return Result().apply {
                code = SUCCESS
                msg = message
                data = datas
                methodName = getMethodName()
            }
        }

        fun fail(): Result {
            return Result().apply {
                code = FAIL
                msg = "fail"
                methodName = getMethodName()
            }
        }

        fun fail(message: String): Result {
            return Result().apply {
                code = FAIL
                msg = message
                methodName = getMethodName()
            }
        }

        private fun getMethodName(): String {
            val methodName = Throwable().run {
                stackTrace.get(2).methodName
            }
            return methodName
        }
    }

    override fun toString(): String {
        return "Result(code=$code, msg=$msg, data=$data, methodName=$methodName)"
    }


}